//
//  FHVisitorView.swift
//  FHweibo
//
//  Created by 张嵩雷 on 16/4/8.
//  Copyright © 2016年 feng. All rights reserved.
//

import UIKit

//代理使用
//1(代理).定义代理协议
protocol FHVisitorViewDelegate: NSObjectProtocol {
    //2(代理).定义代理属性
    func visitorViewDidClickRegisterButton()
    func visitorViewDidClickLoginButton()
}

class FHVisitorView: UIView {
    
    //3(代理)定义代理属性
    weak var delegate: FHVisitorViewDelegate?
    

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        prepareUI()
        
        //设置背景颜色
        backgroundColor = UIColor(red: 237 / 255.0, green: 237 / 255.0, blue: 237 / 255.0, alpha: 1)
        //下面的方法仅适用于颜色的RGB相同
        backgroundColor = UIColor(white: 237 / 255.0, alpha: 1)
    }
    
    // MARK: - 公开方法
    ///转轮动画
    func startRotation() {
    //UIview动画很复杂,
//        UIView.animateWithDuration(2.5) { () -> Void in
//            self.iconView.transform = CGAffineTransformMakeRotation(CGFloat(M_PI * 2))
//        }
        
        
        //使用核心动画
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = M_PI * 2
        anim.repeatCount = MAXFLOAT
        anim.duration = 20
        
        //动画完成不要移除动画
         anim.removedOnCompletion = false
        
        iconView.layer.addAnimation(anim, forKey: nil)
    }
    
    
    
    /// 设置访客视图内容
    /// - parameter imageName: 图片名称
    /// - parameter message:   消息
    func setupVisitorViewInfo(imageName: String,message: String) {
        
        //设置转轮的图片
        iconView.image = UIImage(named: imageName)
        
        //设置消息内容
        messageLabel.text = message
        
        //隐藏小房子
        houseView.hidden = true
        
        //隐藏遮盖
        coverView.hidden = true
        
//        //将一个view弄到最底部
//        self.sendSubviewToBack(coverView)
//        //讲一个view弄到最前面
//        self.bringSubviewToFront(coverView)
    }
    
    ///准备UI
    private func prepareUI() {
        //1.添加控件
        addSubview(iconView)
        addSubview(coverView)
        addSubview(houseView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        //禁用Autoresizing
        iconView.translatesAutoresizingMaskIntoConstraints = false
        coverView.translatesAutoresizingMaskIntoConstraints = false
        houseView.translatesAutoresizingMaskIntoConstraints = false
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        
        //2.添加约束
        ///约束都往父控件上添加
        ///item: 要约束的view
        ///attribute: 要约束的view的属性
        ///relatedby:参照关系
        ///toItem:要参照的view
        ///attribute:参照的view的属性
        
        //将约束添加到父控件身上
        ///转轮
        self.addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: -40))
        
        ///遮盖
            addConstraint(NSLayoutConstraint(item: coverView, attribute: .Leading, relatedBy: .Equal, toItem: self, attribute: .Leading, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: .Top, relatedBy: .Equal, toItem: self, attribute: .Top, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: .Trailing, relatedBy: .Equal, toItem: self, attribute: .Trailing, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: .Bottom, relatedBy: .Equal, toItem: registerButton, attribute: .Bottom, multiplier: 1, constant: 0))
        
        ///房子
        self.addConstraint(NSLayoutConstraint(item: houseView, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: houseView, attribute: .CenterY, relatedBy: .Equal, toItem: iconView, attribute: .CenterY, multiplier: 1, constant: 0))
        
        ///消息label
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Top, relatedBy: .Equal, toItem: iconView, attribute: .Bottom, multiplier: 1, constant: 16))
        ///当参照的view为nil时,attribute必须设置为NSLayoutAttribute.NOtAnAttribute
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 250))
        
        ///注册按钮
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Leading, relatedBy: .Equal, toItem: messageLabel, attribute: .Leading, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Top, relatedBy: .Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 35))
        
        ///登录按钮
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Trailing, relatedBy: .Equal, toItem: messageLabel, attribute: .Trailing, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Top, relatedBy: .Equal, toItem: registerButton, attribute: .Top, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Width, relatedBy: .Equal, toItem: registerButton, attribute: .Width, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Height, relatedBy: .Equal, toItem: registerButton, attribute: .Height, multiplier: 1, constant: 0))
    }
    
    //MARK : - 按钮点击事件
    ///注册按钮点击事件
    @objc private func didClickRegisterButton() {
        //4(代理).调用代理的协议方法
        delegate?.visitorViewDidClickRegisterButton()
    }
    ///登录按钮点击事件
    @objc private func didClickLoginButton() {
        //4(代理).调用代理的协议方法
        delegate?.visitorViewDidClickLoginButton()
        
    }
    
    
    // MARK: - 懒加载
    ///转轮
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    ///遮盖
    private lazy var coverView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    ///小房子
    private lazy var houseView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    ///消息文字
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        
        //设置
        label.text = "关注一些人!看看有神马惊喜!"
        label.font = UIFont.systemFontOfSize(18)
        label.numberOfLines = 0
        label.textAlignment = NSTextAlignment.Center
        
        return label
    }()
    
    ///注册按钮
    private lazy var registerButton: UIButton = {
        let button = UIButton()
        
        //设置
//        button.titleLabel?.text, 按钮不能这样设置文本最终出不来
        button.setTitle("注册", forState: .Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        
        button.addTarget(self, action: Selector("didClickRegisterButton"), forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    
    ///登录按钮
    private lazy var loginButton: UIButton = {
        let button = UIButton()
        button.setTitle("登录", forState: .Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        button.setTitleColor(UIColor.blackColor(), forState: .Normal)
        
        button.addTarget(self, action: Selector("didClickLoginButton"), forControlEvents: UIControlEvents.TouchUpInside)
        
        
        return button
    }()
}
